#include "FindPwdUserInfoVerifyDlg.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <widgets/SLineEdit.h>
#include <widgets/SPopupWindow.h>

#include <QGridLayout>
#include <QLabel>
#include <QPaintDevice>
#include <QPainter>
#include <QPushButton>
#include <QSpacerItem>
#include <QStylePainter>
#include <QWidget>

SUserInfoLabel::SUserInfoLabel(const QString& t, QWidget* parent /*= nullptr*/)
    : QLabel(parent) {
  setText(t);
  const QString labelStyle =
      QString(
          "QLabel "
          "{background-color:#%1;color:#%2;border:none;border-radius:2px;}")
          .arg(StyleMgr::instance().color("StaticTextBK").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("TitleWord").rgb(), 0, 16);
  setStyleSheet(labelStyle);
  setContentsMargins(8, 0, 8, 0);
}

SUserInfoLabel::~SUserInfoLabel() {}

void SUserInfoLabel::setStar(bool hasStar) { bhasStar = hasStar; }

void SUserInfoLabel::paintEvent(QPaintEvent* event) {
  QLabel::paintEvent(event);
  if (bhasStar) {
    QStylePainter painter(this);
    painter.setPen(StyleMgr::instance().color("Up_Increase_H1"));

    QString strStar = "*";

    QRect r = rect();
    QFontMetrics fm(font());
    int widthTxt = fm.horizontalAdvance(strStar);
    QRect startRect;
    startRect.setRect(r.width() - 7 - widthTxt, r.y() + 3, widthTxt,
                      r.height());
    painter.drawText(startRect, strStar);
  }
}

FindPwdUserInfoVerifyDlg::FindPwdUserInfoVerifyDlg(QWidget* p /*= nullptr*/)
    : QWidget(p) {
  // step_2: 用户个人信息验证
  QVBoxLayout* vL = new QVBoxLayout(this);
  vL->setContentsMargins(0, 0, 0, 0);

  QLabel* strVerify =
      new QLabel(tr("完成个人信息验证，继续找回账户密码"), this);
  vL->addWidget(strVerify, 0, Qt::AlignLeft);
  vL->addSpacing(13);

  QGridLayout* gridLayout = new QGridLayout();
  gridLayout->setSpacing(8);
  gridLayout->setVerticalSpacing(24);
  SUserInfoLabel* pLabel = new SUserInfoLabel(tr("机构"), this);
  pLabel->setStar(true);
  pLabel->setFixedSize(85, 24);
  gridLayout->addWidget(pLabel, 0, 0);
  pLabel = new SUserInfoLabel(tr("部门"), this);
  pLabel->setStar(true);
  pLabel->setFixedSize(85, 24);
  gridLayout->addWidget(pLabel, 1, 0);
  pLabel = new SUserInfoLabel(tr("职位"), this);
  pLabel->setStar(true);
  pLabel->setFixedSize(85, 24);
  gridLayout->addWidget(pLabel, 2, 0);
  pLabel = new SUserInfoLabel(tr("固话"), this);
  pLabel->setStar(false);
  pLabel->setFixedSize(85, 24);
  gridLayout->addWidget(pLabel, 3, 0);
  pLabel = new SUserInfoLabel(tr("QQ"), this);
  pLabel->setStar(false);
  pLabel->setFixedSize(85, 24);
  gridLayout->addWidget(pLabel, 4, 0);
  pLabel = new SUserInfoLabel(tr("业务类型"), this);
  pLabel->setStar(false);
  pLabel->setFixedSize(85, 24);
  gridLayout->addWidget(pLabel, 5, 0);

  m_institutionEdit = new SLineEdit("", this);
  m_institutionEdit->setSelectAllWhenFocus(true);
  m_institutionEdit->setFixedSize(292, 24);
  m_institutionEdit->setPlaceholderText(tr("请填写"));
  QFont f = m_institutionEdit->font();
  f.setPixelSize(12);
  m_institutionEdit->setFont(f);
  gridLayout->addWidget(m_institutionEdit, 0, 1);

  m_departmentEdit = new SLineEdit("", this);
  m_departmentEdit->setSelectAllWhenFocus(true);
  m_departmentEdit->setFixedSize(292, 24);
  m_departmentEdit->setPlaceholderText(tr("请填写"));
  m_departmentEdit->setFont(f);
  gridLayout->addWidget(m_departmentEdit, 1, 1);

  m_positionEdit = new SLineEdit("", this);
  m_positionEdit->setSelectAllWhenFocus(true);
  m_positionEdit->setFixedSize(292, 24);
  m_positionEdit->setPlaceholderText(tr("请填写"));
  m_positionEdit->setFont(f);
  gridLayout->addWidget(m_positionEdit, 2, 1);

  m_telEdit = new SLineEdit("", this);
  m_telEdit->setSelectAllWhenFocus(true);
  m_telEdit->setFixedSize(292, 24);
  m_telEdit->setPlaceholderText(tr("请填写"));
  m_telEdit->setFont(f);
  gridLayout->addWidget(m_telEdit, 3, 1);

  m_qqEdit = new SLineEdit("", this);
  m_qqEdit->setSelectAllWhenFocus(true);
  m_qqEdit->setFixedSize(292, 24);
  m_qqEdit->setPlaceholderText(tr("请填写"));
  m_qqEdit->setFont(f);
  gridLayout->addWidget(m_qqEdit, 4, 1);

  m_typeSector = new SCheckComboBox(this, false, "");
  m_typeSector->setFixedSize(292, 24);
  for (int i = 0; i < 8; i++) {
    m_typeSector->addItem(loadTreeData(i));
  }
  gridLayout->addWidget(m_typeSector, 5, 1);
  vL->addLayout(gridLayout);

  m_spBottomItem = new QSpacerItem(0, 20);
  vL->addSpacerItem(m_spBottomItem);
  AddRequiredEdit();  // 将必要的Edit载入vector
}

QString FindPwdUserInfoVerifyDlg::loadTreeData(int i) {
  switch (i) {
    case 0: {
      return tr("交易");
      break;
    }
    case 1: {
      return tr("投资");
      break;
    }
    case 2: {
      return tr("主管领导");
      break;
    }
    case 3: {
      return tr("销售");
      break;
    }
    case 4: {
      return tr("研究");
      break;
    }
    case 5: {
      return tr("中后台");
      break;
    }
    case 6: {
      return tr("IT");
      break;
    }
    case 7: {
      return tr("其他");
      break;
    }
    case 8: {
      return tr("反选");
      break;
    }
    default:
      break;
  }
  return "";
}

void FindPwdUserInfoVerifyDlg::AddRequiredEdit() {
  m_userInfoEdit.push_back(m_institutionEdit);
  m_userInfoEdit.push_back(m_departmentEdit);
  m_userInfoEdit.push_back(m_positionEdit);
}